package wang.z.component;

import wang.z.JFrame.loginFrm;
import wang.z.model.ResultInfo;
import wang.z.net.FailListener;
import wang.z.net.PostUtils;
import wang.z.net.SuccessListener;
import wang.z.utils.JsonUtils;
import wang.z.utils.ResultInfoData2Vector;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * @author like
 * @date 2022/12/11 15:35
 * @Description TODO
 */
public class AdminBookAdd  extends JPanel{
JFrame jf = null;
    private Vector<Vector> AllType = new Vector<Vector>();
    private Vector AllType1 = new Vector();
    private JLabel lblNewLabel;//书名
    private JTextField textField;//书名的文本框

    private JTextField textField_1;//作者

    private JTextField textField_2;//出版社
    private JTextField textField_3;//库存
    private JTextField textField_4;//价格

    private JTextField textField_6;//描述
    private JComboBox comboBox;

    private JButton tBnt;//重置按钮
    private JButton addBtn;//添加按钮


    public  AdminBookAdd(JFrame jf) {
        typeDate();
        this.jf = jf;

        this.setBorder(new TitledBorder(null,
                "\u4E66\u7C4D\u6DFB\u52A0", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED));
        this.setBounds(23, 21, 540, 275);

        this.setLayout(null);
        lblNewLabel = new JLabel("书名：");
        lblNewLabel.setFont(new Font("幼圆", Font.BOLD, 14));
        lblNewLabel.setBounds(58, 31, 45, 27);
        this.add(lblNewLabel);
        textField = new JTextField();
        textField.setBounds(101, 31, 129, 27);
        this.add(textField);
        textField.setColumns(10);

        JLabel label = new JLabel("作者：");
        label.setFont(new Font("幼圆", Font.BOLD, 14));
        label.setBounds(294, 31, 45, 27);
        this.add(label);
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(338, 31, 128, 27);
        this.add(textField_1);

        JLabel label_1 = new JLabel("出版社：");
        label_1.setFont(new Font("幼圆", Font.BOLD, 14));
        label_1.setBounds(43, 79, 60, 27);
        this.add(label_1);
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(101, 79, 129, 27);
        this.add(textField_2);

         JLabel label_2 = new JLabel("库存：");
         label_2.setFont(new Font("幼圆", Font.BOLD, 14));
         label_2.setBounds(58, 125, 45, 27);
        this.add(label_2);
         textField_3 = new JTextField();
         textField_3.setColumns(10);
         textField_3.setBounds(101, 125, 129, 27);
        this.add(textField_3);
         JLabel label_3 = new JLabel("价格：");
         label_3.setFont(new Font("幼圆", Font.BOLD, 14));
         label_3.setBounds(294, 79, 45, 27);
        this.add(label_3);
         textField_4 = new JTextField();
         textField_4.setColumns(10);
         textField_4.setBounds(337, 79, 129, 27);
        this.add(textField_4);
         JLabel label_4 = new JLabel("类别：");
         label_4.setFont(new Font("幼圆", Font.BOLD, 14));
         label_4.setBounds(294, 125, 45, 27);
        comboBox = new JComboBox(AllType1);
        comboBox.setFont(new Font("幼圆", Font.BOLD, 15));
        comboBox.setBounds(337, 125, 129, 27);
        this.add(label_4);
        this.add(comboBox);
         JLabel label_5 = new JLabel("描述：");
         label_5.setFont(new Font("幼圆", Font.BOLD, 14));
         label_5.setBounds(58, 170, 45, 27);
        this.add(label_5);
         textField_6 = new JTextField();
         textField_6.setColumns(10);
         textField_6.setBounds(101, 173, 365, 27);
        this.add(textField_6);
        addBtn = new JButton("添加");
        addBtn.setBounds(130,230,100,27);
        addBtn.setFont(new Font("幼圆", Font.BOLD, 14));
        this.add(addBtn);
        tBnt = new JButton("重置");
        tBnt.setBounds(300,230,100,27);
        tBnt.setFont(new Font("幼圆", Font.BOLD, 14));
        this.add(tBnt);
        jf.add(this);
        jf.add(new JPanel());
        addBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Map<String ,String> params = new HashMap<>();;
                String bookName = textField.getText().trim();
                String author = textField_1.getText().trim();
                String publish = textField_2.getText().trim();
                String number = textField_3.getText().trim();
                String price = textField_4.getText().trim();
                String status = "1";
                String remark = textField_6.getText().trim();
                String typeId = (String) comboBox.getSelectedItem();
                for(Vector vc: AllType){
                    if (vc.get(1).equals(typeId)){
                        typeId = vc.get(0).toString();
                        break;
                    }
                }
                params.put("bookName",bookName);
                params.put("author",author);
                params.put("publish",publish);
                params.put("number",number);
                params.put("price",price);
                params.put("status",status);
                params.put("remark",remark);
                params.put("typeId",typeId);
                PostUtils.postWithParams("http://localhost:8080/book/AddBook", params,new SuccessListener() {
                                //请求发送成功，服务器正常响应，会执行这里
                                @Override
                                public void success(String result) throws IOException {//result参数就是服务器响应回来的json字符串
                                    ResultInfo info = JsonUtils.parseResult(result);
                                    //判断info中的flag标记
                                    if (info.isFlag()) {
                                        JOptionPane.showMessageDialog(jf, info.getMessage());

                                    } else {
                                        JOptionPane.showMessageDialog(jf, info.getMessage());
                                        if(info.getMessage().equals("用户尚未登录")){
                                            new loginFrm().init();
                                            jf.dispose();
                                        }
                                    }
                                }
                            }
                            , new FailListener() {
                                //当该请求发送失败后会执行这个方法
                                @Override
                                public void fail() {
                                    JOptionPane.showMessageDialog(jf, "传入的数据有误或者网路问题，请检查");
                                }
                            }
                    );
                }
        });
        tBnt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setText("");//书名的文本框
                textField_1.setText("");//作者
                textField_2.setText("");//出版社
                textField_3.setText("");//库存
                textField_4.setText("");//价格
                textField_6.setText("");//描述
                comboBox.setSelectedIndex(0);
            }
        });
    }
    public void typeDate() {
        PostUtils.post("http://localhost:8080/book_type/getAll", new SuccessListener() {
            @Override
            public void success(String result) throws IOException {
                ResultInfo info = JsonUtils.parseResult(result);
                if (info.isFlag()) {
                    //消息转换
                    Vector<Vector> vectors = ResultInfoData2Vector.convertResultInfoData2Vector(info);
                    for (Vector vector : vectors) {
                        AllType.add(vector);
                        AllType1.add(vector.get(1));
                    }
                } else {
                    JOptionPane.showMessageDialog(jf, info.getMessage());
                    if(info.getMessage().equals("用户尚未登录")){
                        new loginFrm().init();
                        jf.dispose();
                    }
                }
            }
        }, new FailListener() {
            @Override
            public void fail() {
                JOptionPane.showMessageDialog(jf, "网络异常，请稍后重试");
            }
        });
    }

}
